package xyz.liuz.edb.heart.protocol;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;

public class MessageProtocolEncoder extends MessageToByteEncoder<CoorMessageProtocol> {


    @Override
    protected void encode(ChannelHandlerContext ctx, CoorMessageProtocol msg, ByteBuf out) throws Exception {
        int messageType = msg.getMessageType();
        if(messageType==3){
            byte[] message = msg.getMessage();
            out.writeBytes(message);
        }else{
            out.writeInt(messageType);
            int messageLength = msg.getMessageLength();
            out.writeInt(messageLength);
            byte[] message = msg.getMessage();
            out.writeBytes(message);

        }
    }
}
